package com.woniu.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Order;
import com.woniu.service.IDetailedOrderService;
import com.woniu.service.IOrderService;
import com.woniu.vo.OrderPageVo;
import com.woniu.vo.OrderVo;
import com.woniu.vo.QueryMemberOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author QiuPeng
 * @since 2022-10-12
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单管理")
public class OrderController extends BaseController {
    @Autowired
    private IOrderService iOrderService;
    @Autowired
   private   IDetailedOrderService iDetailedOrderService;


    @PostMapping("/search/{currentPage}/{pageSize}")
    @ApiOperation("分页和搜索查询")
    public Result selectByPageAndLikeSearch(@RequestBody(required = false) OrderPageVo orderPageVo,
                                            @PathVariable Integer currentPage,
                                            @PathVariable Integer pageSize){

       PageResult pageResult =iOrderService.selectByPageAndLikeSearch(orderPageVo,currentPage,pageSize);
       return toDataResult(pageResult);


    }

    @DeleteMapping("/remove/{orderId}")
    @ApiOperation("删除订单")
    public  Result removeById( @PathVariable String orderId ){
        boolean flag = iOrderService.removeById(orderId);
        iDetailedOrderService.removeDetailOrder(orderId);
        return toDataResult(flag,"删除订单成功");
    }



    @GetMapping("/SetAsPaid/{num}/{orderId}")
    @ApiOperation("修改订单状态")
    public Result SetAsPaid(@PathVariable String orderId,@PathVariable String num){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",orderId);
        Order order = new Order();
        order.setOrderStatus(num.toString());
        boolean flag = iOrderService.update(order, queryWrapper);
        return toResult(flag);
    }


    @GetMapping("/member/{memberId}")
    @ApiOperation("通过会员id查找订单信息")
    public Result getByMemberId(@PathVariable String memberId){
        List<QueryMemberOrderVo> vo=iOrderService.getByMemberId(memberId);
        return toDataResult(vo);
    }
    
    @GetMapping("/export")
    @ApiOperation("导出数据")
    public void read( HttpServletResponse response) throws IOException {
        PageResult pageResult = iOrderService.selectByPageAndLikeSearch(null, 1, 1000);
        List<OrderVo> orderList = (List<OrderVo>)pageResult.getData();
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), OrderVo.class).sheet("模板").doWrite(orderList);
    }

}
